﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {

            this.BtnLogin.Attributes.Add("onclick", "return check_submit()");

        }

        if (SysCommon.isUserLogin() == true)
        {
            this.div_login.Visible = false;
        }
        else
        {
            this.div_login.Visible = true;
        }

    }


    protected void BtnLogin_Click(object sender, EventArgs e)
    {

        if (!SysCommon.CheckCodeSame(this.Txt_CheckCode.Text.Trim()))
        {
            SysCommon.JavaScript.alert("校验码错误！请重新输入！");
            return;
        }

        DataRow dr = DataBaseOperate_SQLServer.GetDataRow("select * from t_user where user_name='" + SysCommon.ProcessSqlStr(this.Txt_user_name.Text) + "' and user_password='" + SysCommon.ProcessSqlStr(this.Txt_user_password.Text) + "'");

        if (dr != null)
        {
            Session["user_id"] = dr["user_id"];
            Session["user_name"] = dr["user_name"];
            Session["user_email"] = dr["email"];
            Session["lastlogin_time"] = dr["lastlogin_time"];

            DataBaseOperate_SQLServer.ExecUpdate("update t_user set lastlogin_time=getdate() where user_id=" + Convert.ToString(dr["user_id"]));


            Response.Redirect("~/default.aspx");

        }
        else
        {
            int rowCount = (int)DataBaseOperate_SQLServer.GetValue("select count(1) from t_user where user_name='" + SysCommon.ProcessSqlStr(this.Txt_user_name.Text) + "'");

            if (rowCount == 0)
            {
                SysCommon.JavaScript.alert("用户名输入不正确！请重新输入。");
                return;
            }
            else
            {
                SysCommon.JavaScript.alert("密码输入不正确！请重新输入。");
                return;
            }
        }

    }

}
